package edu.umd.ion.wavemeter.client;

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.ListBox;

public class UnitListBox extends ListBox {
	private double SpeedOfLight = 299792458;
	private boolean ShowUnit = true;
	
	/**
	 *  Allows to convert a Wavelength in nm to the unit selected in a ListBox
	 *  and to formt it into a string.
	 */
	UnitListBox()
	{
		addItem("Wavelength, vac [nm]");
		addItem("Frequency [THz]");
		addItem("Wavenumber [1/cm]");
		addItem("Photon Energy [eV]");
		addItem("Frequency [GHz]");
	}
	
	/**
	 * Append the Unit to the generated string
	 * @param showUnit 
	 */
	UnitListBox(boolean showUnit)
	{
		this();
		ShowUnit = showUnit;
	}
	
	/**
	 * Convert a wavelength mesasurement
	 * @param Wavelength in nm
	 * @return Value in the Unit chosen in the ListBox
	 */
	double convertUnit(double Wavelength)
	{
		switch (getSelectedIndex())
		{
		case 0: return Wavelength;
		case 1: return SpeedOfLight/Wavelength/1000;
		case 2: return 1/Wavelength*1e7;
		case 3: return SpeedOfLight/Wavelength*4.1356668*1e-6;
		case 4: return SpeedOfLight/Wavelength;
		}
		return Wavelength;
	}
	
	/**
	 * Format a wavelength into a string
	 * @param Wavelength in nm
	 * @return formatted string
	 */
	String convert(double Wavelength)
	{
		if (ShowUnit)
		{
			switch (getSelectedIndex())
			{
			case 0: return NumberFormat.getFormat("000.000000").format(Wavelength)+" nm";
			case 1: return NumberFormat.getFormat("000.000000").format(SpeedOfLight/Wavelength/1000)+ " THz";
			case 2: return NumberFormat.getFormat("00000.00000").format(1/Wavelength*1e7)+" /cm" ;
			case 3: return NumberFormat.getFormat("0.00000000").format(SpeedOfLight/Wavelength*4.1356668*1e-6)+" eV";
			case 4: return NumberFormat.getFormat("000000.0000").format(SpeedOfLight/Wavelength)+ " GHz";
			}
		}
		else
			switch (getSelectedIndex())
			{
			case 0: return NumberFormat.getFormat("000.000000").format(Wavelength);
			case 1: return NumberFormat.getFormat("000.000000").format(SpeedOfLight/Wavelength/1000);
			case 2: return NumberFormat.getFormat("00000.0000").format(1/Wavelength*1e7);
			case 3: return NumberFormat.getFormat("0.00000000").format(SpeedOfLight/Wavelength*4.1356668*1e-6);
			case 4: return NumberFormat.getFormat("000000.0000").format(SpeedOfLight/Wavelength);
			}
		return NumberFormat.getFormat("000.000000").format(Wavelength);
	}
}
